﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Services;
using System.IO;
using System.Globalization;

namespace MarginTrading.Forms
{
    public partial class BO_Integration : Form
    {
        public BO_Integration()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            LogService.Add(100, Program.currentUser.UserName, "Uploading account balance file");
            int count = 0;
            try
            {
                using (StreamReader sr = new StreamReader(SampathFile.Text))
                {
                    for (; ; )  // This loop has only one exit.
                    {
                        count++;
                        string line = sr.ReadLine();

                        if (line == null)
                            break;

                        string[] splitLines = line.Split(new char[] { '|' });
                        if (splitLines.Length != 4)
                        {
                            MessageBox.Show("Invalid file. Data mismatch (Line - " + count + ")", "Error");
                        }

                        CultureInfo provider = CultureInfo.InvariantCulture;
                        DateTime TranDate = DateTime.ParseExact(splitLines[0], "ddMMyyyy", provider);

                        if (TrDate.Value.Date != TranDate.Date)
                        {
                            MessageBox.Show("Date mismatch. Please check the file and the selected date", "Error");
                            return;
                        }

                        if (splitLines[2] != "D" && splitLines[2] != "C")
                        {
                            MessageBox.Show("Error in C/D column in file. (Line - " + count + ")", "Error");
                            return;
                        }

                        Double balance = 0.0;
                        if (double.TryParse(splitLines[3], out balance))
                        {
                            //AccBalance.Balance = balance;
                        }
                        else
                        {
                            MessageBox.Show("Error in balance column in file. (Line - " + count + ")", "Error");
                            return;
                        }

                        try
                        {
                            CurrentAccBalance AccBalance = CurrentAccBalanceService.GetAccBalance(TrDate.Value.Date, splitLines[1]);

                            if (AccBalance == null)
                            {
                                AccBalance = new CurrentAccBalance();
                                AccBalance.BalDate = TrDate.Value.Date;
                                AccBalance.AccNo = splitLines[1];
                                AccBalance.DebitCredit = splitLines[2];
                                AccBalance.Balance = balance;

                                CurrentAccBalanceService.Add(AccBalance);

                            }
                            else
                            {
                                AccBalance.DebitCredit = splitLines[2];
                                AccBalance.Balance = balance;
                                CurrentAccBalanceService.Update(AccBalance);
                            }

                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show("Database Error!!");
                            return;
                        }
                    }
                    MessageBox.Show("Done !!", "Message");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            
           

        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            //openFileDialog1.InitialDirectory = "D:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    SampathFile.Text = openFileDialog1.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. " + ex.Message);
                }
            }
        }
    }
}
